package com.enterprise.controller.manage;


import com.enterprise.controller.BaseController;
import com.enterprise.entity.Resources;
import com.enterprise.entity.Respage;
import com.enterprise.entity.Role;
import com.enterprise.entity.page.PageModel;
import com.enterprise.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/manage/role/")
public class RoleAction extends BaseController<Role>{
	private static final String page_toList = "/manage/role/roleList";
	private static final String page_toEdit = "/manage/role/roleInfo";
	private static final String page_toAdd = "/manage/role/roleInfo";
	private static final String page_toResourcesList = "/manage/role/roleResourcesList";
	private static final String page_topageAndBtnList = "/manage/role/pageAndBtnList";
	public RoleAction() {
		super.page_toList = page_toList;
		super.page_toEdit = page_toEdit;
		super.page_toAdd = page_toAdd;
	}

	@Autowired
	private ResourcesService resourcesService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private RoleresService roleresService;

	@Autowired
	private RespageService respageService;


	@Override
	public Services<Role> getService() {
		return roleService;
	}

	// 跳转角色分配权限页面
	@RequestMapping("toRoleResList")
	public String toRoleResList(HttpServletRequest request,@ModelAttribute("e") Respage e){
		String roleid = request.getParameter("roleid");
		this.initPageSelect();
		int offset = 0;
		if(request.getParameter("pager.offset")!=null){
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		if(offset < 0){
			offset=0;
		}
		e.setOffset(offset);
		e.setRoleid(Long.valueOf(roleid));
		PageModel page = respageService.selectPowerPageList(e);
		if(page == null){
			page = new PageModel();
		}
		page.setPagerSize((page.getTotal() + page.getPageSize() - 1)
				/ page.getPageSize());
		selectListAfter(page);
		page.setDataid(Integer.parseInt(roleid));
		request.setAttribute("pager", page);
		return page_topageAndBtnList;
	}


	// 跳转角色分配权限页面
	@RequestMapping("toRoleRes")
	public String toRoleRes(HttpServletRequest request,@ModelAttribute("e") Resources e){
		String roleid = request.getParameter("roleid");
		this.initPageSelect();
		int offset = 0;
		if(request.getParameter("pager.offset")!=null){
			offset = Integer.parseInt(request.getParameter("pager.offset"));
		}
		if(offset < 0){
			offset=0;
		}
		e.setOffset(offset);
		e.setPageSize(100);
		e.setResourcesid(Long.valueOf(roleid));
		PageModel page = resourcesService.selectPowerPageList(e);
		if(page == null){
			page = new PageModel();
		}
		page.setPageSize(page.getTotal());
		page.setPagerSize((page.getTotal() + page.getPageSize() - 1) / page.getPageSize());
		selectListAfter(page);
		page.setPagerUrl("toRoleRes?roleid="+roleid);
		page.setDataid(Integer.parseInt(roleid));
		request.setAttribute("pager", page);
		return page_toResourcesList;
	}


	// 添加权限
	@RequestMapping("setResources")
	public String setResources(HttpServletRequest request, String[] resourcesIds, String roleId, @ModelAttribute("e") Resources e, RedirectAttributes flushAttrs) throws Exception {
		roleresService.setRoleResources(resourcesIds,roleId);
		addMessage(flushAttrs,"操作成功！");
		return "redirect:selectList";
	}

	// 删除
	@RequestMapping("delResources")
	public String delResources(HttpServletRequest request, String[] resourcesIds, String roleId, @ModelAttribute("e") Resources e, RedirectAttributes flushAttrs) throws Exception {
		roleresService.delRoleResources(resourcesIds,roleId);
		addMessage(flushAttrs,"操作成功！");
		return "redirect:selectList";
	}

}
